package com.jushu.storbox.fragment;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;

import com.jushu.storbox.R;
import com.jushu.storbox.ShareDriveMainFragmentActivity;
import com.jushu.storbox.adapter.DownloadListAdapter;
import com.jushu.storbox.entities.FileInfo;
import com.jushu.storbox.service.ICloudDriveService;
import com.jushu.storbox.util.PublicTools;
import com.jushu.storbox.view.pulltorefresh.MyScrollView;
public class DownloadListFragment extends BaseFragment {
	private View viewLayoutInflater;
	private final static String LOG_TAG = "DownloadListFragment";
	private Activity activity;
	private List<FileInfo> list;
	/** cloud drive service */
	private ICloudDriveService shareDriveService;
	private Handler activityHandler;
	private MyScrollView download_list_scrollview;
	private LinearLayout download_list_layout;
	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		setBack(false);
		this.activity = (ShareDriveMainFragmentActivity) activity;
		if (null == getObj()) {
			// System.exit(1);
			PublicTools.restartApp(activity);
		} else {
			Map<String, Object> mp = (Map<String, Object>) getObj();
			if (null != (Handler) mp.get("activityhandler")) {
				activityHandler = (Handler) mp.get("activityhandler");
			} else {
				PublicTools.restartApp(activity);
			}
			if (null != (ICloudDriveService) mp.get("service")) {
				shareDriveService = (ICloudDriveService) mp.get("service");
			} else {
				PublicTools.restartApp(activity);
			}
		}
		super.onAttach(activity);
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		viewLayoutInflater = inflater.inflate(R.layout.fragment_download_list, null);
		setBack(true);
		findByid();
		initData();
		return viewLayoutInflater;
	}
	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		list = shareDriveService.getDownloadFileList(); 
		Log.e("~~", "list.size():"+list.size());
		showMsgData(list);
	}
	private void findByid() {
		// TODO Auto-generated method stub
		download_list_scrollview = (MyScrollView) viewLayoutInflater
				.findViewById(R.id.download_list_scrollview);
		download_list_layout = (LinearLayout) viewLayoutInflater
				.findViewById(R.id.download_list_layout);
	}
	/**
	 * 
	 * @param fileFolderList
	 */
	private void showMsgData(List<FileInfo> fileFolderList) {
		download_list_scrollview.removeAllItem();
		if (null != fileFolderList && 0 < fileFolderList.size()) {
			Log.e("~~", "asdfasdfa:");
			BaseAdapter baseAdapter = new DownloadListAdapter(activity,fileFolderList,shareDriveService,handler) ;
			download_list_scrollview.setAdptor(baseAdapter);
			Log.e("~~", "asdfasdfa1");
			download_list_scrollview.notifyAllItem();
			Log.e("~~", "asdfasdfa2");
		} else {
			download_list_scrollview.resetList();
		}
	}
	private Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 1:  //delete item
				final View item = (View)msg.obj;
				list.remove(item);
				BaseAdapter baseAdapter = new DownloadListAdapter(activity,list,shareDriveService,handler) ;
				download_list_scrollview.setAdptor(baseAdapter);
				download_list_scrollview.removeItem(item);
				break;
			case 2:
				break;
			}
		};
	};
	private void initData(){
		download_list_scrollview.setLayout(download_list_layout);
	}
	
	@Override
	public void onDataLoaded() {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void refreshListFromServer() {
		// TODO Auto-generated method stub
		
	}
}
